package com.ayf.payment.game.api.controller;

import com.ayf.payment.game.api.dto.*;
import com.ayf.payment.game.api.dto.criteria.TemplateCriteria;
import com.ayf.payment.game.api.dto.criteria.TemplateGameSelectCriteria;
import com.ayf.payment.game.api.dto.criteria.TemplatePageCriteria;
import com.ayf.payment.game.api.dto.criteria.TemplateSelectCriteria;
import com.ayf.payment.game.api.entity.Merchant;
import com.ayf.payment.game.api.entity.MerchantConfig;
import com.ayf.payment.game.api.entity.Template;
import com.ayf.payment.game.api.entity.platform.WxConfig;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.TemplateMapper;
import com.ayf.payment.game.api.service.MerchantConfigService;
import com.ayf.payment.game.api.service.MerchantService;
import com.ayf.payment.game.api.service.PartitionService;
import com.ayf.payment.game.api.service.TemplateService;
import com.ayf.payment.game.api.service.platform.WxConfigService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.type.MerchantStatus;
import com.ayf.payment.game.api.type.Status;
import com.ayf.payment.game.api.utils.CommonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 模板控制层
 * @Author: zh-liang
 * @Date : 2019-12-04 14:52
 */
@RestController
@RequestMapping("/v/template")
public class TemplateController {

    @Autowired
    private TemplateService templateService;
    @Autowired
    private MerchantConfigService merchantConfigService;
    @Autowired
    private WxConfigService wxConfigService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private PartitionService partitionService;
    @Resource
    private TemplateMapper templateMapper;

    /**
     * 查询出所有模板
     *
     * @param merchantDTO
     * @param selectCriteria
     * @return
     */
    @PostMapping("/selectPage")
    public CommonResult<PageInfo> selectPageTemplate(MerchantDTO merchantDTO, @RequestBody TemplatePageCriteria selectCriteria) {
        PageHelper.startPage(selectCriteria.getPageNum(), selectCriteria.getPageSize());
        selectCriteria.setMerchantId(merchantDTO.getId());
        selectCriteria.setTemplateStatus(Status.ENABLE);
        List<TemplateDTO> templateDTOS = templateService.selectPageTemplate(selectCriteria);
        PageInfo pageInfo = new PageInfo(templateDTOS);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, pageInfo);
    }

    /**
     * 查询出GM所有模板
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/getTemplateList")
    public CommonResult<List<TemplateDTO>> getTemplateList(MerchantDTO merchantDTO) {
        TemplateSelectCriteria selectCriteria =new TemplateSelectCriteria();
        selectCriteria.setMerchantId(merchantDTO.getId());
        selectCriteria.setTemplateStatus(Status.ENABLE);
        List<TemplateDTO> templateDTOS = templateService.selectAllTemplate(selectCriteria);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, templateDTOS);
    }


    /**
     * 查询出所有模板
     *
     * @param merchantDTO
     * @param selectCriteria
     * @return
     */
    @PostMapping("/selectAll")
    public CommonResult<List<TemplateDTO>> selectAllTemplate(MerchantDTO merchantDTO, @RequestBody TemplateSelectCriteria selectCriteria) {
        selectCriteria.setMerchantId(merchantDTO.getId());
        selectCriteria.setTemplateStatus(Status.ENABLE);
        List<TemplateDTO> templateDTOS = templateService.selectAllTemplate(selectCriteria);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, templateDTOS);
    }

    /**
     * 添加模板
     *
     * @param merchantDTO
     * @param templateCriteria
     * @return
     */
    @PostMapping("/add")
    public CommonResult<Boolean> addTemplate(MerchantDTO merchantDTO, @RequestBody TemplateCriteria templateCriteria) {
        templateCriteria.setMerchantId(merchantDTO.getId());
        templateCriteria.setOperIp(merchantDTO.getOperIp());
        if (templateService.insertTemplate(templateCriteria)) {
            return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, true);
        }
        return new CommonResult<>(CommonConstants.RETURN_ERROR, CommonConstants.OPERATION_ERROR, false);
    }

    /**
     * 查询出模板详细信息
     *
     * @param selectCriteria
     * @return
     */
    @PostMapping("selectTemplateInfo")
    public CommonResult<TemplateCriteria> selectTemplateInfoById(@RequestBody TemplateSelectCriteria selectCriteria) {
        if (selectCriteria == null || StringUtils.isBlank(selectCriteria.getId())) {
            throw new IllegalArgumentException("查询模板详细信息，模板id不能为空");
        }
        TemplateCriteria templateInfo = templateService.selectTemById(selectCriteria.getId());
        return new CommonResult<TemplateCriteria>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, templateInfo);
    }

    /**
     * 更新模板详细信息
     *
     * @param updateCriteria
     * @return
     */
    @PostMapping("/update")
    public CommonResult<Boolean> updateTemplateInfo(MerchantDTO merchantDTO,@RequestBody TemplateCriteria updateCriteria) {
        updateCriteria.setOperIp(merchantDTO.getOperIp());
        if (!merchantDTO.getId().equals(updateCriteria.getMerchantId())){
            return new CommonResult<>(CommonConstants.RETURN_ERROR, CommonConstants.OPERATION_ERROR, false);
        }
        Template template = templateMapper.selectByPrimaryKey(updateCriteria.getId());
        if (template ==null){
            return new CommonResult<>(CommonConstants.RETURN_ERROR, CommonConstants.OPERATION_ERROR, false);
        }
        if (!merchantDTO.getId().equals(template.getMerchantId())){
            return new CommonResult<>(CommonConstants.RETURN_ERROR, CommonConstants.OPERATION_ERROR, false);
        }
        if (templateService.updateTemInfo(updateCriteria)) {
            return new CommonResult<Boolean>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, true);
        }
        return new CommonResult<>(CommonConstants.RETURN_ERROR, CommonConstants.OPERATION_ERROR, false);
    }

    /**
     * 删除模板
     *
     * @param delCriteria
     * @return
     */
    @PostMapping("/delete")
    public CommonResult<Boolean> delTemplateById(MerchantDTO merchantDTO,@RequestBody TemplateCriteria delCriteria) {
        delCriteria.setOperIp(merchantDTO.getOperIp());
        if (templateService.updateTemStatusById(delCriteria)) {
            return new CommonResult<Boolean>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, true);
        }
        return new CommonResult<>(CommonConstants.RETURN_ERROR, CommonConstants.OPERATION_ERROR, false);
    }

    /**
     * 游戏下所有模板查询
     *
     * @param tgc
     * @return
     */
    @PostMapping("/listByGameId")
    public CommonResult<List<GameTemplateDTO>> selectTemplateByGameId(MerchantDTO merchantDTO, @RequestBody TemplateGameSelectCriteria tgc) {
        if (merchantDTO == null) {
            throw new ParameterException("商户没有登录");
        }
        tgc.setMerchantId(merchantDTO.getId());
        List<GameTemplateDTO> dtoList = templateService.selectTemByGameId(tgc);
        return new CommonResult<List<GameTemplateDTO>>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, dtoList);
    }

    /**
     * 查询出分区详情(充值时调用)
     *
     * @param parAndTemInfo
     * @return
     */
    @PostMapping("/selectParAndTemInfo")
    public CommonResult<ParAndTemInfoDTO> selectParAndTemInfo(@RequestBody ParAndTemInfoDTO parAndTemInfo) {
        if (parAndTemInfo == null || StringUtils.isBlank(parAndTemInfo.getPartitionId())) {
            throw new ParameterException("分区id不能为空");
        }
        PartitionDTO info = partitionService.getPartitionInfoById(parAndTemInfo.getPartitionId());
        if (info == null) {
            throw new ParameterException("分区不存在");
        }
        Merchant merchant = merchantService.find(info.getMerchantId());
        if (merchant == null) {
            return new CommonResult<>(405, "当前商户已被禁用，禁止充值");
        }
        if (MerchantStatus.PROHIBIT.name().equals(merchant.getStatus().name()) || MerchantStatus.DELETE.name().equals(merchant.getStatus().name())) {
            return new CommonResult<>(405, "当前商户已被禁用，禁止充值");
        }
        ParAndTemInfoDTO dto = templateService.selectParAndTemInfo(parAndTemInfo.getPartitionId());
        MerchantConfig merchantConfig = merchantConfigService.selectOne(dto.getMerchantId());
        int playerIntegralExStatus=0;
        if (1==merchantConfig.getPlayerIntegralExStatus()&&dto.getIsGiveIntegral()==1){ // 积分开启
            WxConfig wxConfig = wxConfigService.getGameConfigByMerchantRelationId(dto.getMerchantId());
            if (1==wxConfig.getStatus()){
                playerIntegralExStatus = 1;
            }
        }
        dto.setPlayerIntegralStatus(playerIntegralExStatus);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, dto);
    }
}
